package finance

import org.apache.commons.math.stat.correlation.SpearmansCorrelation

class CorrelationService {

    static transactional = true
    def historicalQueryService

    def correlate(data1, data2) {
        def n = data1.size()
        def correlation = new SpearmansCorrelation().correlation(data1 as double[],
            data2 as double[])
        def z = fischer( correlation ) * Math.sqrt( (n - 3)/ 1.06)
        
        return [correlation : correlation, z : z]
    }
    
    def fischer(r) {
        return 0.5 * Math.log( ( 1 + r ) / ( 1 - r ) )
    }
}
